package com.fyfeng.touchtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.Scroller;
import android.widget.Toast;

public class RefreshListView extends ListView {

	private static final String tag = RefreshListView.class.getSimpleName();

	private GestureDetector mGestureDetector;//手势处理类

	private Scroller mScroller;//滑动处理类
	
	private int headContentHeight;//头的高度
	
	private int paddingTop;
	
	private View header;
	
	private Context context;
	
	private boolean isRefreshing = false;
	
	private float percent = 0.35f;
	
	private OnRefreshListener mRefreshListener;

	public RefreshListView(Context context) {
		super(context);
		this.context = context;
		this.init();
	}

	public RefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.init();
	}
	
	public void setOnRefreshListener(OnRefreshListener listener){
		this.mRefreshListener = listener;
	}

	public void refreshCompleted(){
		header.setPadding(header.getPaddingLeft(), -1 * headContentHeight, header.getPaddingRight(), header.getPaddingBottom());
		isRefreshing = false;
		
	}
	
	private void init(){
		
		mGestureDetector = new GestureDetector(context, new MySimpleOnGestureListener(this));
		
		mScroller = new Scroller(context);
		
		LayoutInflater  lif = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		header = lif.inflate(R.layout.refresh_header, null);
		
		headContentHeight = header.getMeasuredHeight();
		
		Log.d(tag, "headContentHeight="+headContentHeight);
		
		headContentHeight = 80;
		
		Log.d(tag, "headContentHeight="+headContentHeight);
		
		header.setPadding(header.getPaddingLeft(), -1 * headContentHeight, header.getPaddingRight(), header.getPaddingBottom());
		
		addHeaderView(header);
		
	}

	public View getHeader(){
		return header;
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_CANCEL: {
			Log.i(tag, "ACTION_CANCEL");
			break;
		}
		case MotionEvent.ACTION_DOWN: {
			Log.i(tag, "ACTION_DOWN：按下触摸.");
			break;
		}
		case MotionEvent.ACTION_MOVE: {
			Log.i(tag, "ACTION_MOVE:触摸滑动.");
			break;
		}
		case MotionEvent.ACTION_OUTSIDE: {
			Log.i(tag, "ACTION_OUTSIDE:滑出边界.");
			break;
		}
		case MotionEvent.ACTION_UP: {
			Log.i(tag, "ACTION_UP：离开触摸.");
			release();
			break;
		}
		}
		
		
		this.mGestureDetector.onTouchEvent(event);
		
		return super.onTouchEvent(event);
	}
	
	/**
	 * 把下拉后的列表复原。
	 */
	private void release(){
		
		Toast.makeText(context, "top:"+header.getPaddingTop(), Toast.LENGTH_SHORT).show();
		
		mRefreshListener.onRefresh();
		
		int ptop = header.getPaddingTop();
		
		Log.d(tag, "ptop="+ptop);
		
		mScroller.startScroll(0, ptop, 0, -ptop);
		
	}

	
	@Override
	public void computeScroll() {
		if(this.mScroller.computeScrollOffset()){
			
			int currY = this.mScroller.getCurrY();
			
			header.setPadding(header.getPaddingLeft(), currY, header.getPaddingRight(), header.getPaddingBottom());
			header.invalidate();
		}

		super.computeScroll();
	}

	private void scrolling(int x, int y){
		
		Log.d(tag, "paddingTop:"+this.paddingTop+", y="+y);
		
		int ptop = header.getPaddingTop() + (int)(-y * percent);
		Log.d(tag, "ptop="+ptop);
		
		header.setPadding(header.getPaddingLeft(), ptop, header.getPaddingRight(), header.getPaddingBottom());
		
	}

	public class MySimpleOnGestureListener extends SimpleOnGestureListener {

		private RefreshListView mListView;

		public MySimpleOnGestureListener(RefreshListView layout) {
			this.mListView = layout;
		}


		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

			float rawX = e1.getRawX();
			float rawY = e1.getRawY();
			float x = e1.getX();
			float y = e1.getY();

			float rawX_e2 = e2.getRawX();
			float rawY_e2 = e2.getRawY();
			float x_e2 = e2.getX();
			float y_e2 = e2.getY();
			
			Log.i(tag, ">>>>---------------------------------");
			Log.i(tag, "onScroll: event1=" + e1.getAction() + ", event2=" + e2.getAction() + ", distanceX=" + distanceX + ", distanceY=" + distanceY);
			Log.i(tag, "onScroll: event1: RawX=" + rawX + ", RawY=" + rawY + ", X=" + x + ", Y=" + y);
			Log.i(tag, "onScroll: event2: RawX=" + rawX_e2 + ", RawY=" + rawY_e2 + ", X=" + x_e2 + ", Y=" + y_e2);
			Log.i(tag, "<<<<---------------------------------");

			mListView.scrolling(0, (int)distanceY);
			
			return true;
		}

	}

	public Scroller getmScroller() {
		return mScroller;
	}
	
	
	public interface OnRefreshListener{
		public void onRefresh();
	}
	
	
	
}
